package cn.zurish.cloud.snow.tools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 实体类转Excel的工具类
 */
public class EntityToExcelConverter {

    /**
     * 将实体类列表转换为Excel文件
     * @param dataList 实体类列表
     * @param excelFilePath Excel文件路径
     * @param <T> 实体类类型
     * @throws IOException 文件操作异常
     */
    public static <T> void convertToExcel(List<T> dataList, String excelFilePath) throws IOException {
        if (dataList == null || dataList.isEmpty()) {
            return;
        }

        // 设置表头样式
        WriteCellStyle headStyle = new WriteCellStyle();
        headStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        WriteFont headFont = new WriteFont();
        headFont.setBold(true);
        headFont.setFontHeightInPoints((short) 12);
        headStyle.setWriteFont(headFont);

        // 设置内容样式
        WriteCellStyle contentStyle = new WriteCellStyle();
        contentStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        // 应用样式
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);

        // 使用EasyExcel直接写入实体类数据
        EasyExcel.write(excelFilePath, dataList.get(0).getClass())
                .registerWriteHandler(styleStrategy)
                .sheet("Data")
                .doWrite(dataList);
    }

    /**
     * 示例实体类
     */
    public static class User {
        @ExcelProperty("用户ID")
        private Long id;

        @ExcelProperty("用户名")
        private String username;

        @ExcelProperty("年龄")
        private Integer age;

        @ExcelProperty("邮箱")
        private String email;

        // 构造函数、getter和setter方法
        public User() {}

        public User(Long id, String username, Integer age, String email) {
            this.id = id;
            this.username = username;
            this.age = age;
            this.email = email;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }
    }

    /**
     * 主方法，用于测试
     */
    public static void main(String[] args) {
        try {
            // 创建测试数据
            List<User> userList = new ArrayList<>();
            userList.add(new User(1L, "张三", 25, "zhangsan@example.com"));
            userList.add(new User(2L, "李四", 30, "lisi@example.com"));
            userList.add(new User(3L, "王五", 28, "wangwu@example.com"));

            // 生成Excel文件
            String excelFilePath = "users.xlsx";
            convertToExcel(userList, excelFilePath);

            System.out.println("实体类数据已成功转换为Excel文件：" + excelFilePath);
        } catch (IOException e) {
            System.err.println("转换过程中发生错误：" + e.getMessage());
            e.printStackTrace();
        }
    }
}
